package utils;

import org.springframework.web.multipart.MultipartFile;
import pojos.common.ImgAuthCode;
import pojos.common.SmsAuthCode;

import javax.servlet.http.HttpSession;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 输入验证工具类
 */
public class InputVerifyUtils {

    /**
     * 图片验证码验证
     */
    public static boolean isImgAuthCodeNotRight(HttpSession session, String authCode){
        // 是否为空验证
        if(authCode == null){
            return true;
        }

        // 长度验证
        if(authCode.length() != 4){
            return true;
        }

        // 是否过期验证
        Object imgObj = session.getAttribute("ImgAuthCode");
        if(imgObj == null){
            return true;
        }

        ImgAuthCode imgAuthCode = (ImgAuthCode) imgObj;

        // 是否过期验证
        long time = imgAuthCode.getGenerateTime();
        long current = System.currentTimeMillis();
        if(current - time > 300000){
            return true;
        }

        // 正确性验证
        String code = imgAuthCode.getAuthCode();
        return !(code.equalsIgnoreCase(authCode));
    }

    /**
     * 登录名验证
     */
    public static boolean isLoginNameNotRight(String loginName){
        // 是否为空验证
        if(loginName == null){
            return true;
        }

        int loginNameLen = loginName.length();
        return (loginNameLen < 2 || loginNameLen > 20);
    }

    /**
     * 登录密码验证
     */
    public static boolean isPasswordNotRight(String password){
        // 是否为空验证
        if(password == null){
            return true;
        }

        return (password.length() != 32);
    }

    /**
     * 手机号码正确性验证
     */
    public static boolean isPhoneNotRight(String phone){
        // 是否为空验证
        if(phone == null){
            return true;
        }

        String regex = "^((13[0-9])|(14[579])|(15([0-3,5-9]))|(16[6])|(17[0135678])|(18[0-9]|19[89]))\\d{8}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        return !(m.matches());
    }

    /**
     * 手机验证码正确性验证
     */
    public static boolean isSmsCodeNotRight(HttpSession session, String smsCode){
        if(smsCode == null){
            return true;
        }

        // 长度验证
        if(smsCode.length() != 6){
            return true;
        }

        // 是否过期验证
        Object smsObj = session.getAttribute("SmsAuthCode");
        if(smsObj == null){
            return true;
        }

        SmsAuthCode smsAuthCode = (SmsAuthCode) smsObj;

        // 过期验证
        long generateTime = smsAuthCode.getGenerateTime();
        long currentTime = System.currentTimeMillis();
        if(currentTime - generateTime > 300000){
            return true;
        }

        // 正确性验证
        String sSmsCode = smsAuthCode.getAuthCode();
        return !(sSmsCode.equals(smsCode));
    }

    /**
     * 真实姓名验证
     */
    public static boolean isRealNameNotRight(String name){
        if(name == null){
            return true;
        }

        Pattern pattern = Pattern.compile("^[\u4E00-\u9FA5\uf900-\ufa2d·]{2,20}$");
        Matcher matcher = pattern.matcher(name);
        return !(matcher.matches());
    }

    /**
     * 上传头像验证
     */
    public static boolean isAvatarNotRight(MultipartFile file){
        // 为空判断
        if(file == null){
            return true;
        }

        // 文件大小判断
        long fileSize = file.getSize();
        if(fileSize <= 0 || fileSize > 2097152){
            return true;
        }

        // 文件名判断
        String fileName = file.getOriginalFilename();
        if(fileName == null){
            return true;
        }

        // 文件类型判断（根据后缀名）
        String fileType = fileName.substring(fileName.indexOf(".")+1);
        Pattern pattern = Pattern.compile("^(gif|jpeg|jpg|png|bmp)$");
        Matcher m = pattern.matcher(fileType);
        return !(m.matches());
    }

    /**
     * 邮箱验证
     */
    public static boolean isEmailNotRight(String email){
        // 为空判断
        if(email == null){
            return true;
        }

        // 正确性验证
        Pattern pattern = Pattern.compile("^([a-zA-Z0-9]+[_.\\-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_.\\-]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$");
        Matcher matcher = pattern.matcher(email);
        return !(matcher.matches());
    }

    /**
     * QQ号验证
     */
    public static boolean isQQNotRight(String QQ){
        // 为空判断
        if(QQ == null) {
            return true;
        }

        // 正确性判断
        Pattern pattern = Pattern.compile("^\\d{5,11}$");
        Matcher matcher = pattern.matcher(QQ);
        return !(matcher.matches());
    }

    /**
     * 微信号正确性判断
     */
    public static boolean isWeChatNotRight(String weChat){
        // 为空判断
        if(weChat == null){
            return true;
        }

        // 正确性判断
        Pattern pattern = Pattern.compile("^[a-zA-Z]([-_a-zA-Z0-9]{5,20})+$");
        Matcher matcher = pattern.matcher(weChat);
        return !(matcher.matches());
    }

    /**
     * 学校名称正确性判断
     */
    public static boolean isSchoolNotRight(String school){
        // 为空判断
        if(school == null){
            return true;
        }

        // 正确性判断
        Pattern pattern = Pattern.compile("^[\u4E00-\u9FA5\uF900-\uFA2D()（）]{3,20}$");
        Matcher matcher = pattern.matcher(school);
        return !(matcher.matches());
    }

    /**
     * 专业名称正确性验证
     */
    public static boolean isMajorNotRight(String major){
        // 为空验证
        if(major == null){
            return true;
        }

        // 正确性验证
        Pattern pattern = Pattern.compile("^[\u4E00-\u9FA5\uF900-\uFA2D()（）]{2,20}$");
        Matcher matcher = pattern.matcher(major);
        return !(matcher.matches());
    }

    /**
     * 学历验证
     */
    public static Integer getRightEdu(Integer edu){
        if(edu == null){
            return null;
        }

        if(edu >= 0 && edu <= 3){
            return edu;
        }

        return null;
    }

    /**
     * 教师年级验证
     */
    public static Integer getTeacherGrade(Integer grade){
        if(grade == null){
            return null;
        }

        if(grade >=0 && grade <= 4){
            return grade;
        }

        return null;
    }
}
